package com.library.test;

import java.io.IOException;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.library.slayer.model.LMSBook;
import com.library.slayer.model.impl.LMSBookImpl;
import com.library.slayer.service.LMSBookLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;

@Controller(value = "LibraryPortlet")  
@RequestMapping("VIEW")  
public class LibraryPortlet {
    @RenderMapping  
    public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){  
       
     return "view";  
    }
    @RenderMapping(params = "action=update")
    public String update() {
    	return "update";
    }
    @RenderMapping(params = "action=details")
    public String details() {
    	return "details";
    }
    @RenderMapping(params = "action=list")
    public String list() {
    	return "list";
    }
    @ActionMapping(params = "action=updateBook")
    public void updateBook(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, SystemException, PortalException{
    	long bookId = ParamUtil.getLong(actionRequest, "bookId");
    	String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");
		LMSBook book = null;
		
		if (bookId > 0L) {
			book = LMSBookLocalServiceUtil.getLMSBook(bookId);
			book.setBookId(bookId);
			book.setBookTitle(bookTitle);
			book.setAuthor(author);
			book.setCreateDate(new Date());
			modifyBook(book);
		} else {
			book = new LMSBookImpl();
			
			bookId = CounterLocalServiceUtil.increment(LMSBook.class.getName());
			book.setBookId(bookId);
			book.setBookTitle(bookTitle);
			book.setAuthor(author);
			book.setCreateDate(new Date());
			addBook(book);
		}
		
		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
		actionResponse.sendRedirect(redirectURL);
		
	}

	private void addBook(LMSBook book) {
		try {
			
			LMSBookLocalServiceUtil.addLMSBook(book);
			
		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
	
	private void modifyBook(LMSBook book) {
			
		try {
			LMSBookLocalServiceUtil.updateLMSBook(book);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 @ActionMapping(params = "action=deleteBook")
	    public void deleteBook(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, SystemException, PortalException{
		 long bookId = ParamUtil.getLong(actionRequest, "bookId");
		 
		 if (bookId > 0L) {
			 LMSBookLocalServiceUtil.deleteLMSBook(bookId);
		 
			 String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
				actionResponse.sendRedirect(redirectURL);
		 }
	 }

}
